package com.yixing.demo.component;

import com.yixing.demo.constants.Constants;
import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;

import java.security.Key;
import java.util.Date;
import java.util.Map;
@Slf4j
public class JwtClass {
    private static final long Expiration = 120 * 60 * 1000;

    private static final String encodeKey = "k9hdc5hYhXNVKNrVzgPLloPQuiI0QNbFSRL2Hk35Hg0=";
    private static final Key key = Keys.hmacShaKeyFor(Decoders.BASE64URL.decode(encodeKey));

    public static String getToken(Map<String, Object> map) {
        return Jwts.builder()
                .setClaims(map)
                .signWith(key)
                .setExpiration(new Date(System.currentTimeMillis() + Expiration))//过期时间
                .compact();
    }

    public static Claims parseToken(String token) {
        JwtParser build = Jwts.parserBuilder().setSigningKey(key).build();
        Claims body = null;
        try {
            body = build.parseClaimsJws(token).getBody();
        } catch (ExpiredJwtException e) {
             log.error("token超时");
        } catch (Exception e) {
            log.error("token校验失败");
        }
        return body;
    }
}
